package com.ruoyi.mes.service.impl;

import java.util.List;

import com.ruoyi.common.core.domain.AjaxResult;
import com.ruoyi.common.utils.DateUtils;
import com.ruoyi.mes.domain.HouInfo;
import com.ruoyi.mes.mapper.HouInfoMapper;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import com.ruoyi.mes.mapper.MtrStockMapper;
import com.ruoyi.mes.domain.MtrStock;
import com.ruoyi.mes.service.IMtrStockService;

/**
 * 物料库存Service业务层处理
 *
 * @author LongAndHao
 * @date 2023-04-04
 */
@Service
public class MtrStockServiceImpl implements IMtrStockService
{
    @Autowired
    private MtrStockMapper mtrStockMapper;

    @Autowired
    private HouInfoMapper houInfoMapper;


    /**
     * 查询物料库存
     *
     * @param id 物料库存主键
     * @return 物料库存
     */
    @Override
    public MtrStock selectMtrStockById(Long id)
    {
        return mtrStockMapper.selectMtrStockById(id);
    }

    /**
     * 查询物料库存列表
     *
     * @param mtrStock 物料库存
     * @return 物料库存
     */
    @Override
    public List<MtrStock> selectMtrStockList(MtrStock mtrStock)
    {
        return mtrStockMapper.selectMtrStockList(mtrStock);
    }

    /**
     * 新增物料库存
     *
     * @param mtrStock 物料库存
     * @return 结果
     */
    @Override
    public AjaxResult insertMtrStock(MtrStock mtrStock)
    {
        //新增时判断库房剩余容量是否充足
        HouInfo houInfo = houInfoMapper.selectHouInfoByHouId(mtrStock.getHouId());
        if (houInfo.getHouRemainCapacity() < mtrStock.getMtrQuantity()){
            return AjaxResult.error("库房剩余容量不足!");
        }

        mtrStock.setCreateTime(DateUtils.getNowDate());
        mtrStockMapper.insertMtrStock(mtrStock);
        return AjaxResult.success("新增成功!");
    }

    /**
     * 修改物料库存
     *
     * @param mtrStock 物料库存
     * @return 结果
     */
    @Override
    public int updateMtrStock(MtrStock mtrStock)
    {
        mtrStock.setUpdateTime(DateUtils.getNowDate());
        return mtrStockMapper.updateMtrStock(mtrStock);
    }

    /**
     * 批量删除物料库存
     *
     * @param ids 需要删除的物料库存主键
     * @return 结果
     */
    @Override
    public int deleteMtrStockByIds(Long[] ids)
    {
        return mtrStockMapper.deleteMtrStockByIds(ids);
    }

    /**
     * 删除物料库存信息
     *
     * @param id 物料库存主键
     * @return 结果
     */
    @Override
    public int deleteMtrStockById(Long id)
    {
        return mtrStockMapper.deleteMtrStockById(id);
    }
}
